/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dinni.webservicestest.util;

import com.dinni.webservicestest.dbaccess.User;
import com.dinni.webservicestest.dbaccess.WebServicesDbHelper;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Dinni
 */
public class WebServiceHelper {

    private static final WebServiceHelper singleInstance;
    public static String htmlHeader = "<html> " + "<title>" + "WebServicesTest" + "</title>";
    public static String htmlBody = "<body><h3>";
    public static String htmlBodyClosingTag = "</h3></body>";
    public static String htmlHeaderClosingTag = "</html> ";
    public static String defaultBodyString = "give your query in <em>?username=dsc&password=pass</em> format";
    public static String xmlHeader = "<?xml version=\"1.0\"?><user>";
    public static String xmlClosing = "</user>";
    public static final HashMap userHash = new HashMap<String, User>(10);
    private static Pattern emailPattern;
    private static Matcher emailMatcher;
    private static final String EMAIL_PATTERN =
            //"^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
    "^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$";
    //"/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/"

    static {
        try {
            //populate userHash for debugging without db access
            userHash.put("dsc110@gmail.com", new User("dsc110@gmail.com", "pass", "R.B.I.Layout, Bengaluru, Karnataka"));
            userHash.put("arun@gmail.com", new User("arun@gmail.com", "pass", "Brigade Millennium , 7th Phase J.P.Nagar, Bangalore, Karnataka 560078"));
            userHash.put("prashant@gmail.com", new User("prashant@gmail.com", "pass", "Dollars Layout Bangalore, Karnataka"));
            userHash.put("vijay@gmail.com", new User("vijay@gmail.com", "pass", "Domlur, Bangalore, Karnataka"));
            //userHash.put("apple@gmail.com", new User("apple@gmail.com","pass", "LalBagh Garden, Bangalore, Karnataka"));
            //userHash.put("orange@gmail.com", new User("orange@gmail.com","pass", "LalBagh Garden, Bangalore, Karnataka"));

            singleInstance = new WebServiceHelper();
            emailPattern = Pattern.compile(EMAIL_PATTERN);


        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Cannot Load WebServiceConstants." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }    

    public static String formatUserOutputToHtml(User user, String username, String password) {
        if (user == null) //return makeBodyString(username, debug) + "\n" + "The password you sent is" + password;
        {
            return "Hi " + username + " you are not a recorded user,please enter valid username and password";
        }
        if (user.getUname().equals("User")) {//if user name is User ,return message stored in address field for errors 
            return "Hi " + username + "We have a error :<br></br>" + user.getAddress();
        }
        if (user.getPw().equals(password)) //return makeBodyString(username, debug) + "\n" + "The password you sent is" + password;
        {
            return "Hi " + username + " and welcome " + "<br></br>"
                    + "  your password :" + password + "<br></br>" + "   your address :" + user.getAddress();

        }
        //if came till here then only password is incorrect ...
        return "Hi " + username + " please enter valid username and password";
    }

    public static String formatUserOutputToJson(User user, String username, String password) {

        if (user == null) {
            return "{\"username\":\"" + username + "\", \"message\":\"you are not a recorded user,please enter valid username and password\"}";
        }
        if (user.getUname().equals("User")) {//if user name is User ,return message stored in address field for errors 
            return "{\"username\":\"" + username + "\", \"message\":\"We have a error :" + user.getAddress() + "\"}";
        }
        if (user.getPw().equals(password)) {

            return "{\"username\":\"" + username + "\",\"password\":\"" + password + "\",\"address\":\"" + user.getAddress() + "\", \"message\":\"welcome\"}";
        }
        //if came till here then only password is incorrect ...
        return "{\"username\":\"" + username + "\", \"message\":\"please enter valid username and password\"}";
    }

    public static String formatUserOutputToXml(User user, String username, String password) {
        if (user == null) {
            return xmlHeader + "<username>" + username + "</username><message>you are not a recorded user,please enter valid username and password</message>" + xmlClosing;
        }
        if (user.getUname().equals("User")) {//if user name is User ,return message stored in address field for errors 
            return xmlHeader + "<username>" + username + "</username><message>We have a error :" + user.getAddress() + "</message>" + xmlClosing;
        }
        if (user.getPw().equals(password)) //return makeBodyString(username, debug) + "\n" + "The password you sent is" + password;
        {

            return xmlHeader + "<username>" + username + "</username><password>" + password + "</password><address>" + user.getAddress() + "</address><message>welcome</message>" + xmlClosing;
        }
        //if came till here then only password is incorrect ...
        return xmlHeader + "<username>" + username + "</username><message>please enter valid username and password</message>" + xmlClosing;
    }
    public static boolean isValidEmail(String emailString) {
        emailMatcher = emailPattern.matcher(emailString);
        boolean match =emailMatcher.matches();
        return match;
    }
     /*public static WebServiceHelper getWebServiceConstants() {
        return singleInstance;
    }*/
}
